//
//  UIView+Rounded.m
//  WHPersonalData
//
//  Created by shengmeng chen on 2022/11/9.
//  Copyright © 2022 mac. All rights reserved.
//

#import "UIView+Rounded.h"

@implementation UIView (Rounded)

/// 给view加圆角
/// - Parameters:
///   - radius: 圆角大小
///   - byRoundingCorners: 圆角位置
///   - view: 要加圆角的view
+(void)setViewRoundedWithRad:(CGFloat)radius byRoundingCorners:(UIRectCorner) corner view:(UIView *) view{
    //加延时 是因为如果view还没有设置约束(frame)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = view.bounds;
        maskLayer.path = path.CGPath;
        view.layer.mask = maskLayer;
        });
}


/// 给view加圆角和阴影
/// - Parameters:
///   - radius: 圆角大小
///   - corner: 圆角位置
-(void)setViewRoundedShadowWithRad:(CGFloat)radius byRoundingCorners:(UIRectCorner) corner{
    //加延时 是因为如果view还没有设置约束(frame)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = path.CGPath;
        self.layer.mask = maskLayer;
        
        CALayer *subLayer = [CALayer layer];
        subLayer.frame = CGRectMake(self.frame.origin.x+.1, self.frame.origin.y+.1, self.frame.size.width-.2, self.frame.size.height-.2);
        subLayer.cornerRadius=radius;
        subLayer.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.8].CGColor;
        subLayer.masksToBounds=NO;
        subLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
        subLayer.shadowOffset = CGSizeMake(1.5, 1.5);//shadowOffset阴影偏移,x向右偏移3，y向下偏移2，默认(0, -3),这个跟shadowRadius配合使用
        subLayer.shadowOpacity = 0.3;//阴影透明度，默认0
        subLayer.shadowRadius = 2;//阴影半径，默认10
        
        [[self superview].layer insertSublayer:subLayer below:self.layer];
        });
}


/// 给view加边框
/// - Parameters:
///   - borderWidth: 边框宽度
///   - color: 边框颜色
///   - cornerRadius: 边框圆角度数
-(void)setViewBorderWithBorderWidth:(CGFloat)borderWidth color:(UIColor *) color cornerRadius:(CGFloat)cornerRadius{
    self.layer.borderWidth  = borderWidth;
    self.layer.borderColor  = color.CGColor;
    self.layer.cornerRadius = cornerRadius;
}

- (void)addDottedLineFromColor:(UIColor *) color{
    //加延时 是因为如果view还没有设置约束(frame)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.layer.cornerRadius = 4;
        UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer*borderLayer = [[CAShapeLayer alloc]init];
        borderLayer.bounds= self.bounds;
        borderLayer.frame = self.bounds;
        borderLayer.path = path.CGPath;
        borderLayer.lineWidth=0.5;
        
        //虚线边框---小边框的长度
        borderLayer.lineDashPattern = @[@6,@4]; //前边是虚线的长度，后边是虚线之间空隙的长度
        borderLayer.lineDashPhase=0.1;
        borderLayer.fillColor=UIColor.clearColor.CGColor;
        borderLayer.strokeColor=color.CGColor;
        [self.layer addSublayer:borderLayer];
        
        
        });
}
@end
